#!/usr/local/bin/php
<?php
defined('OPEN_LIB_PATH') || define('OPEN_LIB_PATH', '/opt/op/OpenLib');

require_once OPEN_LIB_PATH . '/open/models/OaAppModel.php';

$logger = Log::factory('console', '', '', array(
    'lineFormat' => '[%1$s] [%3$s] %5$s(%6$s): %4$s',
    'timeFormat' => '%Y-%m-%d %H:%M:%S'
), PEAR_LOG_INFO);
$logger->setPriority(PEAR_LOG_ERR);
$dbConfig = Oa_Util_Config::getSingleDbConfig(OA_WEB_DB_CLUSTER_ID);
$oaWebDb = OpDbModel::getInstance($dbConfig, $logger);

$options = getopt('g:o:');
if (empty($options)) {
    $logger->err("usage: " . basename(__FILE__) . " -g group -o add|update|delete");
    exit();
}
if (!isset($options['g'])) {
    $logger->err("no group");
    exit();
}
$groupId = (int) $options['g'];
if ($groupId <= 0) {
    $logger->err("group invalid: group-$groupId");
    exit();
}
if (!isset($options['o'])) {
    $logger->err("no action");
    exit();
}
$op = $options['o'];
if (!in_array($op, array('add', 'update', 'delete'))) {
    $logger->err("invalid action: action-$op");
    exit();
}

$appModel = new OaAppModel(OaAppModel::APP_CONFIG_ENV_PRODUCTION, $logger);
if (!OaSyncInfoLogic::syncGroupInfo($groupId, $op)) {
    $logger->err("sync group info failed: group-$groupId, op-$op");
} else {
    $logger->info("sync group info succeed: group-$groupId, op-$op");
}
